import re

url = re.compile(r'''
    (ht|f)tps?://                                           # Protocol
    (?:\w+:\w+@)?                                           # Username:Password
    ([-\w]+)(\.[-\w]+)*                                     # domain
    ([\d]{1,5})?                                            # Port
    (                                                       # Directories
        ((/([-\w~!$+|.,=]   |
        %[a-f\d]{2})+)+|/)+ |
        \?                  |
        [#]
    )?  
    (                                                       # Query
        (
            \?([-\w~!$+|.,*:]   |
            %[a-f\d{2}])+=
            ([-\w~!$+|.,*:=]|%[a-f\d]{2})*
        )
        (&
            ([-\w~!$+|.,*:]|%[a-f\d{2}])+
            =([-\w~!$+|.,*:=]|%[a-f\d]{2})*
        )*
    )*
    ([#](?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?                  # Anchor
    ''',
    re.VERBOSE
)

email = re.compile(r'''
    (
        ^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+
        (\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*  |                     # dot-atom
        ^"(
            [\001-\010\013\014\016-\037!#-\[\]-\177]   |
            \\[\001-\011\013\014\016-\177]
        )*"                                                      # quoted-string
    )
    @(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$                                # domain
    ''', 
    re.IGNORECASE | re.VERBOSE  
)
